package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
@Autowired
private DeptService deptService;
@Autowired
private EmpExprService empExprService;

    @Override
    public PageResult pagelist(EmpQueryParam param) {
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
        Page page=new Page(param.getPage(), param.getPageSize());
        // 调用Service层查询分页数据
        this.lambdaQuery().like(StringUtils.hasLength(name), Emp::getName, name)
                .eq(gender != null, Emp::getGender, gender)
                .between(begin != null && end != null, Emp::getEntryDate, begin, end).page(page);
        long total = page.getTotal();
        List<Emp> empList = page.getRecords();
   /*     Set<Integer> detpList=new HashSet<>();
        for (Emp o : empList) {
           detpList.add(o.getDeptId());
        }*/
        List<Dept> list = deptService.list();
        empList.forEach(emp->{
            for (Dept dept : list) {
                if (dept.getId()==emp.getDeptId()){
                    emp.setDeptName(dept.getName());
                }
            }
        });
        return new PageResult<>(total,empList);

    }
@Transactional(rollbackFor = Exception.class)
    @Override
    public void saveService(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        this.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        for (EmpExpr empExpr : exprList) {
            empExpr.setEmpId(emp.getId());
        }
        empExprService.saveBatch(exprList);

    }
@Transactional(rollbackFor = Exception.class)
    @Override
    public void delteService(List<Integer> ids) {
        List<Emp> list = this.lambdaQuery().in(Emp::getId, ids).list();
        empExprService.remove(Wrappers.<EmpExpr>lambdaQuery().in(EmpExpr::getEmpId,ids));
        this.removeBatchByIds(ids);
    }
@Transactional(rollbackFor = Exception.class)
    @Override
    public Emp getByIdService(Integer id) {
        Emp emp = this.getById(id);
        List<EmpExpr> list = empExprService.lambdaQuery().eq(EmpExpr::getEmpId, id).list();
        emp.setExprList(list);
        return emp;
    }

    @Override
    public void updateService(Emp emp) {
        // 调用service层方法修改员工信息
        // 根据id更新员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);

        // 根据id批量删除员工的工作经历信息
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));

        // 批量保存员工的工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()) {
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);
    }


}
